/**   
 *    
 * 项目名称：SmartKungFuService   
 * 类名称：MstTransactionImpl   
 * 类描述：  流水交易记录实现类 
 * 创建人：zhangl  
 * 创建时间：2016年12月27日 下午7:22:14   
 * 修改人：zhangl   
 * 修改时间：2016年12月27日 下午7:22:14   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.lst.common.CommCode;
import com.lst.dao.MstTransactionMapper;
import com.lst.model.MstTransaction;
import com.lst.service.MstTransactionService;
import com.lst.utils.DateUtil;

/**
 * @ClassName: MstTransactionImpl
 * @Description: 流水交易记录实现类
 * @author zhangl
 * @date 2016年12月27日 下午7:22:14
 *
 */
@Service("mstTransactionService")
public class MstTransactionImpl implements MstTransactionService {

	private Logger logger = Logger.getLogger("log");

	@Autowired
	private MstTransactionMapper dao;

	/* (非 Javadoc)
	 * <p>Title: deleteByPrimaryKey</p>
	 * <p>Description: </p>
	 * @param id
	 * @return
	 * @see com.lst.service.MstTransactionService#deleteByPrimaryKey(java.lang.Integer)
	 */
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return dao.deleteByPrimaryKey(id);
	}

	/* (非 Javadoc)
	 * <p>Title: insert</p>
	 * <p>Description: </p>
	 * @param record
	 * @return
	 * @see com.lst.service.MstTransactionService#insert(com.lst.model.MstTransaction)
	 */
	@Override
	public int insert(MstTransaction record) {
		return dao.insert(record);
	}

	/* (非 Javadoc)
	 * <p>Title: insertSelective</p>
	 * <p>Description: </p>
	 * @param record
	 * @return
	 * @see com.lst.service.MstTransactionService#insertSelective(com.lst.model.MstTransaction)
	 */
	@Override
	public int insertSelective(MstTransaction record) {
		return dao.insertSelective(record);
	}

	/* (非 Javadoc)
	 * <p>Title: selectByPrimaryKey</p>
	 * <p>Description: </p>
	 * @param id
	 * @return
	 * @see com.lst.service.MstTransactionService#selectByPrimaryKey(java.lang.Integer)
	 */
	@Override
	public MstTransaction selectByPrimaryKey(Integer id) {
		return dao.selectByPrimaryKey(id);
	}

	/* (非 Javadoc)
	 * <p>Title: updateByPrimaryKeySelective</p>
	 * <p>Description: </p>
	 * @param record
	 * @return
	 * @see com.lst.service.MstTransactionService#updateByPrimaryKeySelective(com.lst.model.MstTransaction)
	 */
	@Override
	public int updateByPrimaryKeySelective(MstTransaction record) {
		return dao.updateByPrimaryKeySelective(record);
	}

	/* (非 Javadoc)
	 * <p>Title: updateByPrimaryKey</p>
	 * <p>Description: </p>
	 * @param record
	 * @return
	 * @see com.lst.service.MstTransactionService#updateByPrimaryKey(com.lst.model.MstTransaction)
	 */
	@Override
	public int updateByPrimaryKey(MstTransaction record) {
		return dao.updateByPrimaryKey(record);
	}

	/**
	 * 
	 * @Title: insert
	 * @Description: 支付宝流水记录
	 * @param @param params
	 * @param @return    设定文件
	 * @return String    返回类型
	 */
	public String insertByAli(Map<String, String> params){
		Date startDate = new Date();
		logger.info("insert begin: " + startDate);

		String reulst = "";
		try {
			
			Map<String, Object> decMap = new HashMap<String, Object>();
			decMap.put("enabled", true);
			decMap.put("isDeleted", false);
			decMap.put("tradeNo", params.get("trade_no"));
			
			List<MstTransaction> list = dao.queryByList(decMap, new PageBounds());
			
			if(list.isEmpty()){
				MstTransaction mstTransaction = new MstTransaction();
				mstTransaction.setCreatedate(startDate);
				mstTransaction.setEnabled(true);
				mstTransaction.setIsdeleted(false);
				mstTransaction.setReserved1(CommCode.SYSTEM_PAY_TYPE_ALI);
				
				for (String key : params.keySet()) {
					String value = params.get(key);
					if (!StringUtils.isEmpty(key)) {
						if("notify_time".equals(key)){
							mstTransaction.setNotifytime(DateUtil.parse(value, DateUtil.ALL_DATETIME_HORIZONTAL));
						}

						if("notify_type".equals(key)){
							mstTransaction.setNotifytype(value);
						}

						if("notify_id".equals(key)){
							mstTransaction.setNotifyid(value);
						}

						if("sign_type".equals(key)){
							mstTransaction.setSigntype(value);
						}

						if("trade_no".equals(key)){
							mstTransaction.setTradeno(value);
						}

						if("out_trade_no".equals(key)){
							mstTransaction.setOuttradeno(value);
						}

						if("buyer_id".equals(key)){
							mstTransaction.setBuyerid(value);
						}
						
						if("buyer_email".equals(key)){
							mstTransaction.setBuyerlogonid(value);
						}

						if("seller_id".equals(key)){
							mstTransaction.setSellerid(value);
						}

						if("seller_email".equals(key)){
							mstTransaction.setSelleremail(value);
						}

						if("trade_status".equals(key)){
							mstTransaction.setGradestatus(value);
						}
						
						if("quantity".equals(key)){
							mstTransaction.setTotalamount(Long.valueOf(value));
						}
						
						if("price".equals(key)){
							mstTransaction.setReserved3(new BigDecimal(value));
						}
						
						if("total_fee".equals(key)){
							mstTransaction.setReserved4(new BigDecimal(value));
						}
					}
				}

				dao.insert(mstTransaction);
			} 
			
			//更新交易状态等信息
			if(!list.isEmpty() && !"TRADE_SUCCESS".equals(list.get(0).getGradestatus())){
			
				MstTransaction mstTransaction = new MstTransaction();
				
				mstTransaction.setUpdatedate(startDate);
				
				for (String key : params.keySet()) {
					String value = params.get(key);
					if (!StringUtils.isEmpty(key)) {
						if("trade_status".equals(key)){
							mstTransaction.setGradestatus(value);
						}
					}
				}
				
				mstTransaction.setId(list.get(0).getId());

				dao.updateByPrimaryKeySelective(mstTransaction);
			}

			reulst = "SUCCESS"; 
		} catch (Exception e) {
			logger.error("insert error:" ,e);
			reulst = "ERROR"; 
		}

		logger.info("insert end spent(s): " + DateUtil.calLastedTime(startDate));

		return reulst;
	}
	
	/**
	 * 
	 * @Title: insertByWeix
	 * @Description: 微信流水记录
	 * @param @param params
	 * @param @return    设定文件
	 * @return String    返回类型
	 */
	public String insertByWeix(Map<String, String> params){
		Date startDate = new Date();
		logger.info("insert begin: " + startDate);

		String reulst = "";
		try {
			
			Map<String, Object> decMap = new HashMap<String, Object>();
			decMap.put("enabled", true);
			decMap.put("isDeleted", false);
			decMap.put("tradeNo", params.get("out_trade_no"));
			
			List<MstTransaction> list = dao.queryByList(decMap, new PageBounds());
			
			if(list.isEmpty()){
				MstTransaction mstTransaction = new MstTransaction();
				mstTransaction.setCreatedate(startDate);
				mstTransaction.setEnabled(true);
				mstTransaction.setIsdeleted(false);
				mstTransaction.setReserved1(CommCode.SYSTEM_PAY_TYPE_WEIX);
				
				mstTransaction.setNotifytime(startDate);
				mstTransaction.setNotifytype("trade_status_sync");
				mstTransaction.setNotifyid(null);
				mstTransaction.setSigntype("MD5");
				for (String key : params.keySet()) {
					String value = params.get(key);
					if (!StringUtils.isEmpty(key)) {

						if("transaction_id".equals(key)){
							mstTransaction.setTradeno(value);
						}

						if("out_trade_no".equals(key)){
							mstTransaction.setOuttradeno(value);
						}

						/*if("buyer_id".equals(key)){
							mstTransaction.setBuyerid(value);
						}*/
						
						if("openid".equals(key)){
							mstTransaction.setBuyerlogonid(value);
						}

						if("mch_id".equals(key)){
							mstTransaction.setSellerid(value);
						}

						/*if("seller_email".equals(key)){
							mstTransaction.setSelleremail(value);
						}*/

						if("result_code".equals(key)){
							mstTransaction.setGradestatus(value);
						}
						
						/*if("quantity".equals(key)){
							mstTransaction.setTotalamount(Long.valueOf(value));
						}*/
						
						if("cash_fee".equals(key)){
							mstTransaction.setReserved4(new BigDecimal(value).multiply(new BigDecimal(100)));
						}
						
					}
				}

				dao.insert(mstTransaction);
			}

			reulst = "SUCCESS"; 
		} catch (Exception e) {
			logger.error("insert error:" ,e);
			reulst = "ERROR"; 
		}

		logger.info("insert end spent(s): " + DateUtil.calLastedTime(startDate));

		return reulst;
	}

	/**
	 * 
	 * @Title: queryByList
	 * @Description: 查询结果集
	 * @param @param map
	 * @param @param pageBounds
	 * @param @return    设定文件
	 * @return List<MstTransaction>    返回类型
	 */
	public List<MstTransaction> queryByList(Map<String, Object> map,PageBounds pageBounds){
		return dao.queryByList(map, pageBounds);
	}
}
